function initWidgets()
{
	$.get("php/firmendatenbank.php", "mode=init&part=spselect", function(data) {
		document.getElementById("spselect").innerHTML = data;
	});

	$.get("php/firmendatenbank.php", "mode=init&part=stortselect", function(data) {
		document.getElementById("stortselect").innerHTML = data;
	});

	$.get("php/firmendatenbank.php", "mode=init&part=subjselect", function(data) {
		document.getElementById("subjselect").innerHTML = data;
	});

	$.get("php/firmendatenbank.php", "mode=init&part=resulttbl", function(data) {
		document.getElementById("resulttbl").getElementsByTagName(
		"tbody")[0].innerHTML = data;
		
		initResultTable();
	});

	onChgFilter();
}


function onExportXML() {
	window.open("php/firmendatenbank.php?mode=xmlexp",'Download');
}

function onResultTblTrIn()
{
	if (this.parentNode.nodeName == "THEAD")
		return
	
	var tTd = this.getElementsByTagName("td")[0];

	$(this).addClass("highlight");	
	
	if(tTd!=null)
		tTd.onclick = onTblCompClk;			
}
	

function onResultTblTrOut()
{
	if (this.parentNode.nodeName != "THEAD")
		$(this).removeClass("highlight");
}

function initResultTable() {
	$("#resulttbl").tablesorter();

	$("#resulttbl tr").hover(onResultTblTrIn,
							 onResultTblTrOut);
}


function onChgFilter() {
	var tMultSelect = document.getElementById("spselect");
	var tOptString = "";

	if (tMultSelect == null)
		return;

	for (var i = 0; i < tMultSelect.length; i++) {
		if (tMultSelect[i].selected)
			tOptString += "sp[]=" + tMultSelect[i].value + "&";
	}
	
	tMultSelect = document.getElementById("stortselect");

	if (tMultSelect == null)
		return;

	for (i = 0; i < tMultSelect.length; i++) {
		if (tMultSelect[i].selected)
			tOptString += "so[]=" + tMultSelect[i].value + "&";
	}

	tMultSelect = document.getElementById("subjselect");

	if (tMultSelect == null)
		return;
	
	for (i = 0; i < tMultSelect.length; i++) {
		if (tMultSelect[i].selected)
			tOptString += "th[]=" + tMultSelect[i].value + "&";
	}

	tMultSelect = document.getElementById("bwselect");

	tOptString += "bw=" + tMultSelect[tMultSelect.selectedIndex].value;

	$.get("php/firmendatenbank.php","mode=chgfilter&" + tOptString, function(data) {
				document.getElementById("resulttbl").getElementsByTagName(
						"tbody")[0].innerHTML = data;
				initResultTable();
			});
}

function onTblCompClk() {
	
	$.get("php/firmendatenbank.php?mode=compinfo&company=" + this.innerHTML, null,
			function(data) {
				document.getElementById("details").innerHTML = data;
			});
}


function onWriteComment()
{
	var tOptString="mode=writecom&";
	var tNeueBew=document.getElementById("neueBew");
	var tNeuerKom=document.getElementById("neuerKom");
	var tElmt=document.getElementById("detFirmaName");
	
	tOptString+="bw="+tNeueBew[tNeueBew.selectedIndex].value;
	
	if(tNeuerKom.value=="")
	{		
		if (!confirm("Kommentarfeld leer. Trotzdem senden?"))
			return;
	}
	else
		tOptString+="&text="+tNeuerKom.value;
	
	
	tOptString+="&comp="+tElmt.innerHTML;		
		
	$.get(	"php/firmendatenbank.php?"+tOptString,
			null, function(data) {
		
		$.get("php/firmendatenbank.php?mode=compinfo&company=" + tElmt.innerHTML, null,
				function(data) {
					document.getElementById("details").innerHTML = data;
					
					onChgFilter();
					initResultTable();
				});		
			}
		 );	
}
